import glfw
from glfw import _GLFWwindow as GLFWwindow
import OpenGL.GL as gl
import glm
import imgui
from imgui.integrations.glfw import GlfwRenderer

class App:
    def __init__(self, xpos: int, ypos: int, width: int, height: int, 
                 title: str) -> None:
        if not glfw.init():
            print("初始化GLFW错误！")
            return
        glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
        glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 5)
        glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
        self.window = glfw.create_window(width, height, title, None, None)
        if not self.window:
            print("创建窗口失败！")
            glfw.terminate()
            return
        glfw.set_window_pos(self.window, xpos, ypos)
        glfw.make_context_current(self.window)
        # 设置背景颜色
        self.background_color = glm.vec4(0.8, 0.8, 0.8, 1.0)
        # 初始化imgui
        imgui.create_context()
        self.impl = GlfwRenderer(self.window, False)
        # 设置显示中文，字体为黑体
        io = imgui.get_io()
        fonts = io.fonts
        font_name = "C:/Windows/Fonts/simhei.ttf"
        self.cn_font = io.fonts.add_font_from_file_ttf(font_name, 
                    16, None, fonts.get_glyph_ranges_chinese_full())
        self.impl.refresh_font_texture()
        self.imgui_window_width = 300
        self.imgui_window_height = 180
        # 处理窗口大小变化
        glfw.set_window_size_callback(self.window, self.resize)
        self.width, self.height = glfw.get_window_size(self.window)
        # 处理鼠标移动
        glfw.set_cursor_pos_callback(self.window, self.mouse_move)
        # 处理鼠标按键
        glfw.set_mouse_button_callback(self.window, self.mouse_click)
        # 处理鼠标滚轮
        glfw.set_scroll_callback(self.window, self.mouse_wheel)

    # 初始化着色器，vao vbo等
    def setupVertex(self) -> None:
        pass

    # 显示imgui界面
    def showImGui(self)->None:
        imgui.begin("设置")
        imgui.text("窗口宽度：{}，窗口高度：{}".format(self.width, self.height))
        xpos, ypos = glfw.get_cursor_pos(self.window)
        imgui.text("鼠标X：{}，鼠标Y：{}".format(xpos, ypos))
        imgui.end() 
        
    # 渲染图形
    def render(self) -> None:
        gl.glClearNamedFramebufferfv(0, gl.GL_COLOR, 0, self.background_color.to_tuple())

    # 释放资源
    def cleanup(self) -> None:
        pass

    def resize(self, window:GLFWwindow, width:int, height:int)-> None:
        self.width = width
        self.height = height
        gl.glViewport(0, 0, width, height)

    def keyInput(self)-> None:
        if glfw.get_key(self.window, glfw.KEY_ESCAPE) == glfw.PRESS:
            glfw.set_window_should_close(self.window, True)

    def mouse_wheel(self, window:GLFWwindow, xoffset:float, 
                    yoffset:float)-> None:
        pass

    def mouse_move(self, window:GLFWwindow, x:float, y:float)->None:
        pass

    def mouse_click(self, window:GLFWwindow, button:int, action:int, 
                    mods:int)->None:
        pass

    def run(self) -> None:
        self.setupVertex()
        while not glfw.window_should_close(self.window):
            self.keyInput()
            self.render()
            self.impl.process_inputs()
            imgui.new_frame()
            imgui.push_font(self.cn_font)
            imgui.set_next_window_position(0, 0, 
                                condition=imgui.FIRST_USE_EVER)
            imgui.set_next_window_size(self.imgui_window_width, 
                                       self.imgui_window_height, 
                                       condition=imgui.FIRST_USE_EVER)
            self.showImGui()
            imgui.pop_font()
            imgui.render()
            self.impl.render(imgui.get_draw_data())
            glfw.swap_buffers(self.window)
            glfw.poll_events()

        self.cleanup()
        self.impl.shutdown()
        glfw.destroy_window(self.window)
        glfw.terminate()

if __name__ == "__main__":
    app = App(100, 100, 800, 600, "你好，App V2.0！")
    app.run()

